import axios from 'axios';
import url from './url';
import qs from 'qs';
import {authToken} from '../globalVariables';
//const ajaxUrl = url + '/api'

let ajax = axios.create({
  baseURL: url,//ajaxUrl
  timeout: 30000
});

ajax.defaults.withCredentials = true;
ajax.interceptors.request.use(config => {
  if (config.url === '/qiniu' || config.url === '/headImg') { } else {
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
    if (config.method === 'post') {
        config.data['authtoken'] = localStorage.getItem(authToken);
        config.data = qs.stringify(config.data)
    }
  }

  return config
}, error => {
  loadinginstace.close();
  return Promise.reject(error)
});

ajax.interceptors.response.use(function (response) {
    // token 已过期，重定向到登录页面
    if (response.data.code === 800) {
        // localStorage.clear();
        // this.$router.replace('/');
    }
    return response
},function (error) {
    // Do something with response error
    console.log(error);
    return Promise.reject(error)
});
export default ajax